# encoding: utf-8
"""
@author: youfeng
@file: zhixing_view.py
@time: 2018/9/16 下午3:00
"""

from flask import Blueprint, has_request_context, request

from common.data_instance import data_inst
from common.log_instance import global_log as log
from ext.response_wrap import ResponseWrap
from service.zhixing_service import ZhixingService
from user.check_auth import CheckAuth

bp = Blueprint('zhixing', __name__, url_prefix='/api/zhixing')
zhixing_instance = ZhixingService(data_inst, log)


@bp.route('', methods=['POST'])
def get_zhixing():
    if not has_request_context():
        log.warn("上下文异常")
        return ResponseWrap.fail(ResponseWrap.HTTP_OK, u"服务器未知!")

    if not request.is_json:
        log.warn("参数错误...")
        return ResponseWrap.fail(ResponseWrap.HTTP_OK, u"need application/json!!")

    company = request.json.get('company')
    user = request.json.get("user")
    app_key = request.json.get('app_key')
    code = CheckAuth.check_user(user, app_key)
    if code != CheckAuth.CODE_SUCCESS:
        return ResponseWrap.fail(ResponseWrap.HTTP_OK, CheckAuth.get_error_msg(code))

    return zhixing_instance.get_data(company)
